package com.chen.controller;

import com.chen.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/user")
@RestController
public class USerController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("findById")
    public User findById( Integer userId){
        ServiceInstance choose = loadBalancerClient.choose("nacos-provider");
        String url = "http://"+choose.getHost()+":"+choose.getPort()+"/user/findById/"+userId;
        return restTemplate.getForObject(url, User.class);
    }




}
